import os

# -*- coding: utf-8 -*-
"""
Created on Wed Nov 20 23:28:00 2019

@author: 1
"""

import os


# 删除空文件夹，否则会报错
def traverse(filepath):
    # 遍历filepath下所有文件，包括子目录
    files = os.listdir(filepath)
    for fi in files:
        fi_d = os.path.join(filepath, fi)
        if os.path.isdir(fi_d):  # 判断是否为文件夹
            if not os.listdir(fi_d):  # 如果文件夹为空
                os.rmdir(fi_d)  # 删除这个空文件夹
            else:
                traverse(fi_d)
        else:
            # file = os.path.join(filepath, fi_d) #重复加入路径，报错
            if os.path.getsize(fi_d) == 0:  # 文件大小为0
                os.remove(fi_d)  # 删除这个文件


def get_file(path):  # 创建一个空列表
    traverse(path)
    files = os.listdir(path)
    files.sort()  # 排序
    list = []
    for file in files:
        if not os.path.isdir(path + file):  # 判断该文件是否是一个文件夹
            # f_name = str(file)
            #    print(f_name)
            # tr = '\\'  # 多增加一个斜杠
            filename = os.path.join(path, file)
            list.append(filename)
    return list


if __name__ == '__main__':
    path = r'data/pattern/'
    traverse(path)
    list = get_file(path)
    print(list)


class PicturesReader:
    file_paths = []  # 文件列表
    file_index = 0  # 文件索引
    image_dict = {}  # 指令显示图片，查找字典，用索引值（图片名第一个_之前的整数值）

    def FolderCurrent(self, fileIndex=None):
        if not fileIndex == None:
            self.file_index = fileIndex
        if self.file_index >= len(self.file_paths):
            self.file_index = len(self.file_paths) - 1
        elif self.file_index < 0:
            self.file_index = 0
        if len(self.file_paths) <= 0 or self.file_index >= len(self.file_paths):
            return None
        cur_path = self.file_paths[self.file_index]
        # self.ProcessImage(cur_path)
        return cur_path

    # 导入文件夹
    def OpenDirectory(self, picturesPath=''):
        # cur_dir = QDir.currentPath()  # 获取当前文件夹路径
        # 删除空文件夹
        traverse(picturesPath)
        # 选择文件夹
        # dir_path = QFileDialog.getExistingDirectory(None,'打开文件夹',r'../data/pictures/')
        # 读取文件夹文件
        self.file_paths.clear()
        for root, dirs, files in os.walk(picturesPath, topdown=False):
            for file in files:
                if file.lower().endswith(
                        ('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')):
                    img_path = os.path.join(root, file)
                    self.file_paths.append(img_path)
                    self.image_dict[int(file.split('_')[0])] = img_path
        # print('image_dict', self.image_dict)
        if len(self.file_paths) <= 0:
            return None
        # 获取第一个文件
        self.file_index = 0
        self.file_paths = sorted(self.file_paths, key=str.lower)
        # 处理文件
        # self.ProcessImage(cur_path)
        return self.file_paths

    # 下一个文件
    def FolderNext(self):
        # 文件索引累加 1
        self.file_index += 1
        if self.file_index >= len(self.file_paths):
            self.file_index = len(self.file_paths) - 1
        if len(self.file_paths) <= 0 or self.file_index >= len(self.file_paths):
            return None
        cur_path = self.file_paths[self.file_index]
        # self.ProcessImage(cur_path)
        return cur_path

    # 上一个文件
    def FolderPrevious(self):
        # 文件索引减 1
        self.file_index -= 1
        if self.file_index < 0:
            self.file_index = 0
        if len(self.file_paths) <= 0 or self.file_index >= len(self.file_paths):
            return None
        # 当前路径
        cur_path = self.file_paths[self.file_index]
        # self.ProcessImage(cur_path)
        return cur_path
